package com.aorise.controller.general;

import com.aorise.exception.DataValidationException;
import com.aorise.service.bayonet.BayonetStatisticsService;
import com.aorise.service.general.GeneralService;
import com.aorise.utils.StatusDefine;
import com.aorise.utils.StatusDefineMessage;
import com.aorise.utils.json.JsonResponseData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.dao.DataAccessException;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Auther: zhouhao
 * @Date: 2019/6/5
 * @Description:
 */
@Slf4j
@Component
@Configuration
@EnableScheduling
public class SaticSchedule {

    @Autowired
    GeneralService generalService;

    @Autowired
    BayonetStatisticsService bayonetStatisticsService;

//    @Scheduled(cron = "0 0 1 * * ?")
    @Scheduled(cron = "0 */3 * * * ?")
    private void insertStatistics(){
        try {
            log.info("每天同步数据开始,当前时间为: {}",new Date());
            generalService.insertStatistics();
            log.info("每天同步数据结束,当前时间为: {}",new Date());
        }catch (DataValidationException e) {
            log.error(
                    "controller:SaticSchedule. function:insertStatistics...msg: insertStatistics DataValidationException");
            log.error("error:" + e.getMessage());
        }catch (DataAccessException e) {
            log.error(
                    "controller:SaticSchedule. function:insertStatistics...msg: insertStatistics DataAccessException");
            log.error("error:" + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error(
                    "controller:SaticSchedule. function:insertStatistics. msg:insertStatistics Exception");
            log.error(e.getMessage());
        }

    }

//    @Scheduled(cron = "0 0 */1 * * ? ")
    @Scheduled(cron = "0 */3 * * * ?")
    private void updateStatistics(){
        try {
            log.info("每小时同步数据开始,当前时间为: {}",new Date());
            generalService.updateStatistics();
            log.info("每小时同步数据结束,当前时间为: {}",new Date());
        }catch (DataValidationException e) {
            log.error(
                    "controller:SaticSchedule. function:updateStatistics...msg: updateStatistics DataValidationException");
            log.error("error:" + e.getMessage());
        }catch (DataAccessException e) {
            log.error(
                    "controller:SaticSchedule. function:updateStatistics...msg: updateStatistics DataAccessException");
            log.error("error:" + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error(
                    "controller:SaticSchedule. function:updateStatistics. msg:updateStatistics Exception");
            log.error(e.getMessage());
        }
    }

//    @Scheduled(cron = "0 0 1 * * ?")
//    @Scheduled(cron = "*/5 * * * * ?")
    @Scheduled(cron = "0 */3 * * * ?")
    private void bayonetStatisticsService(){
        try {
            log.info("每天添加卡口统计开始,当前时间为: {}",new Date());
            bayonetStatisticsService.bayonetStatisticsService();
            log.info("每天添加卡口统计结束,当前时间为: {}",new Date());
        }catch (DataValidationException e) {
            log.error(
                    "controller:SaticSchedule. function:bayonetStatisticsService...msg: bayonetStatisticsService DataValidationException");
            log.error("error:" + e.getMessage());
        }catch (DataAccessException e) {
            log.error(
                    "controller:SaticSchedule. function:bayonetStatisticsService...msg: bayonetStatisticsService DataAccessException");
            log.error("error:" + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error(
                    "controller:SaticSchedule. function:bayonetStatisticsService. msg:bayonetStatisticsService Exception");
            log.error(e.getMessage());
        }

    }
}
